Introduction to Docker Containers
Creating a container
When we call
docker container run
It performs two tasks:
- Creating container
- Starting the container
We can create container by
docker container create
We can start container by
docker container start
When we create container from an image, we can specify its configuration. It means we can specify the evironment variables, arguments, name, etc.
Understanding the command run command:
docker container run --rm -it ubuntu:latest /bin/bash
--rm
this option tells docker to delete the container when it exits.-i
option tells docker that the current session will be interactive and that we want to keep STDIN open.-t
option tells docker to allocate a pseudo-TTY
Container configuration
Name
To give a container name use
docker container create --name="my-cool-container" ubuntu:latest
Labels
Lables are keyvalue pairs in Dockerfile (image).
We can specify label by using option flag -l
or --label
.
docker container run --rm -d --name="container-with-label" \
-l deployer=Vikas -l tester=Vikas \
ubuntu:latest
Hostname
By default the hostname is same as the container id.
We can set the hostname by
docker container run --rm --it \
--hostname="mycontainer" ubuntu:latest /bin/bash
Domain Name Server
TODO
Storage volumes
When the default disk space allocated to a container is not appropriate for the job at hand, In these cases we need storage that can persist between container deployments.
In such case we use --mount
or -v
option. For example,
docker container run --rm --it \
--mount=type=bind,target=/mnt/session_data,source=/data \
ubuntu:latest /bin/bash
- When you use a bind mount, a file or directory on the host machine is mounted into a container.
- The file or directory is referenced by its absolute path on the host machine.
- Neither the host mount point nor the mount point in the container needs to preexist for this command to work properly. If the host mount point does not exists, then it will be created as a directory.
You can read more about the bind mount here.