Watch:
docker container run
(Official documentation)Usage:
docker container run [OPTIONS] image-name [COMMAND] [ARG...]
docker container run
:flag | purpose | example usage |
---|---|---|
--name |
specify a name for the container | --name hello |
-p / --publish |
expose a port to localhost (externalport:internalport) | -p 8080:80 |
-d |
detached session (runs in background) | -d |
--rm |
automatically remove container once stopped | --rm |
-it |
use an interactive session (e.g. bash) | -it / -i -t |
Let's run a container based on the nginx image, in detached mode. We want to publish the contents of that container's internal port 80, to my localhost at port 8080.
Let's run a container based on the alpine image and name it "juice". We want to run it with an interactive terminal, and use the command sh
.
Let's run a container based on the ubuntu image and name it "greet_doggo". We want the container to be removed automatically once it is stopped. Let's have it use the command echo hi juice
.
docker container run -p 8080:80 -d nginx
docker container run -it --name juice alpine sh
docker container run --name greet_doggo --rm ubuntu echo hi juice
docker container ls
(Official documentation)Purpose: list containers (can be used interchangeably with docker ps
)
-a
flag includes stopped containersUsage:
docker container ls [OPTIONS]
docker container stop
(Official documentation)Purpose: stop a container (or containers) that is currently running.
Usage:
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
docker container start
(Official documentation)Purpose: start a stopped container (or containers)
Usage:
docker container start [OPTIONS] CONTAINER [CONTAINER...]
docker container rm
(Official documentation)Purpose: remove a container
-f
flag forces removal even if container is runningUsage:
docker container rm [OPTIONS] CONTAINER [CONTAINER...]
docker container prune
(Official documentation)Purpose: remove all stopped containers
Usage:
docker container prune [OPTIONS]
docker container exec
(Official documentation)Purpose: execute a command within a running container
-it
flag as container run)docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
Watch:
Networks allow containers to communicate with each other, whether or not they are exposing ports to the host.
Official documentation
By default, containers are connected to the "bridge" network.
However, that network won't allow containers to communicate with one another—by default, containers are isolated.
Creating a custom network will allow the containers on that network to interact with each other.
docker network create [OPTIONS] network-name
docker network ls
Let's create a network, and attach containers to it so we can see how networked containers communicate.
For comparison, we'll use two containers that are on the default network.
# create a network based on the bridge driver, called "test-network"
docker network create --driver bridge test-network
# create two images on that network
docker container run -d --name c1 --network test-network nginx:alpine
docker container run -d --name c2 --network test-network nginx:alpine
# create two more images, without specifying a network
docker container run -d --name c3 nginx:alpine
docker container run -d --name c4 nginx:alpine
# access the shell on one of our two networked containers
docker container exec -it c1 ash
# ping a container that is not on the network
ping -c 2 c3
# ping a container that is on the network
ping -c 2 c2
docker container exec -it c3 ash
ping -c 2 c1
ping -c 2 c4
Watch:
Using bind mounts and volumes allows data to persist even after a container is gone.
Bind mounts directly mount the contents of a folder on your filesystem into your container.
Volumes are managed by Docker—so you wouldn't directly access the files, but can be accessed and modified from within a container.
There are two different types of syntax you can use with docker container run
to establish volumes and bind mounts. Both flags can create either volumes or bind mounts.
-v
and --mount
flags have the same purpose.--mount
is typically preferred as it is considered to be clearer.docker container run -v ...
docker container run --mount ...
-v
/--volume
syntax-v
/--volume
syntax-v absolute/path:/absolute/path/in/container
-v
/--volume
syntax-v name:/absolute/path/in/container
--mount
syntaxPass in a series of <key>=<value>
pairs in any order, separated by a comma.
Type must be "bind" for bind mounts, or "volume" for volumes
--mount
syntax--mount type=bind,source=/absolute/path,target=/absolute/path/in/container
--mount type=volume,source=name_of_volume,target=/absolute/path/in/container
First, create a folder called app in your current directory, and make an empty index.html file inside the folder.
# run an nginx:alpine-based container, in detached mode # mount the /app folder into the container at the path where nginx serves # its files from (/usr/share/nginx/html) # and expose port 80 to view the contents docker container run -d \ --mount type=bind,source="$(pwd)/app",target=/usr/share/nginx/html \ -p 8080:80 nginx:alpine
Once your container is running, visit localhost:8080—you should see a blank page. Add some content to your html page, then save and refresh localhost:8080. Now you can see the new content you added, because the contents of your folder stays in sync with the contents of the folder inside your container.
Official documentation
Volumes are managed from within Docker—don't depend on your file structure.
You wouldn't modify the contents directly (only from inside a container), but you can use it to spin up new containers with same contents.
Instead of providing a path to the folder (like you would with a bind mount) you can provide the name of the volume.
# pull the postgres image so we can inspect it docker pull postgres # inspect the image to find out what the path to the volume should be # and what port we want to expose docker image inspect postgres
# run the container (no volume) docker container run -p 5431:5432 \ -e POSTGRES_PASSWORD=password \ --name postgres5431 -d postgres # run the container with a volume named "postgres-data" # that corresponds to the path where a postgres container stores its data (/var/lib/postgresql/data) docker container run -p 5431:5432 \ -e POSTGRES_PASSWORD=password \ --name postgres5431 -d \ --mount type=volume,source=postgres-data,target=/var/lib/postgresql/data postgres # now use the psql command line tool to connect # to the postgres instance running in our container psql -p 5431 -h localhost -U postgres -W