Under the Hood, `docker run` are 2 commands:
docker create+docker start
- a container layer is created on the top of an image
- the container just created is started
It's important to understand this to avoid confusion between docker run, docker start, and docker create
2/12
Docker RUN Common Options:
--name: assign a name
--rm: remove it when it exits
-p: publish ports
-e: set environment variable
-d: run it in the background
--network: connect it to a network
-i: keep stdin open
--mount: set a volume or bind mount
--user: set a user
3/12
Assign a name (--name)
By default, a random name is assigned, but in general is a good idea to assign a custom name to our containers, as we do to our pets!
4/12
Remove the container when exits (--rm)
Useful for disposable containers and examples. When a container is stopped, it’s automatically removed. Handy
5/12
Publish Ports (-p)
To expose a port or a series of ports on the Host Machine.
You can also specify a protocol, for example -p 8080:80/tcp to Map TCP port 80 in the container to port 8080 in the Host
6/12
Environment Variables (-e)
Sets an environment variable in the container. It can also overwrite existing environment variables defined in the Dockerfile
7/12
Run in background (-d)
To run a container in the background, leaving the terminal free. The container ID will be printed on the terminal for further uses
8/12
Connect to a Network (--network)
Connects to a network.
To attach a running container to an existing network:
Docker network connect
To disconnect a running container from a network:
Docker network disconnect
9/12
Keep STDIN open (-i)
In some cases, a very useful option to access directly on the terminal of the container. Often used in combination with -t, to allocate a pseudo-TTY
10/12
Start a container with a volume (--mount)
Docker volumes are the preferred way to persist data in Docker.
Docker run --mount source=volume1,target=/path
Please note that even if you can use the -v option, the --mount is the new preferred one
11/12
Use a custom User (--user)
Specifies a username for the container, Very useful in some cases to avoid using the default one defined in the image
12/12