Skip to content

Dockerfile instructions: FROM, WORKDIR, COPY, RUN, EXPOSE, USER

Select an icon to see what it is.

Step 6 of 6

USER and CMD finish the config

USER nobody makes the container's process run as that user instead of root, and a named user must already exist in the image. USER and CMD, which sets the default command, only change the config and add no filesystem layer. The final image is the base layers plus the layers from WORKDIR, COPY and RUN, and a config with all these settings.

$ docker run --rm idemo
PrevNext

docker image inspect idemo (excerpt)

{
"Config": {
"WorkingDir": "/app"
"ExposedPorts": {
"3000/tcp": {}
}
"User": "nobody"
"Cmd": [
"./app.sh"
]
}
}

Try it yourself

Build the image, read its config, and see what EXPOSE does and does not do.

$ printf '#!/bin/sh\necho hello from $(pwd) as $(id -un)\n' > app.sh
$ printf 'FROM alpine\nWORKDIR /app\nCOPY app.sh .\nRUN chmod +x app.sh\nEXPOSE 3000\nUSER nobody\nCMD ["./app.sh"]\n' > Dockerfile
$ docker build -t idemo .
$ docker image inspect idemo --format 'dir={{.Config.WorkingDir}} user={{.Config.User}} ports={{.Config.ExposedPorts}} cmd={{.Config.Cmd}}'
$ docker run --rm idemo
$ docker run -d -P --name idemo-ctr idemo sleep 100
$ docker port idemo-ctr
$ docker rm -f idemo-ctr

Quiz

1. Does EXPOSE make a port reachable from the host?
2. Which is usually better, COPY or ADD?
3. Which instruction does not add a filesystem layer?