Docker References
This document provides bare minimum references to Docker and Docker Compose to get started in working environment. If extra information is required then it is recommended to check the official Docker documentations.
Docker Basic Commands
1. Pulling image from Docker Hub
Usage
$ docker pull image:tag
Example
$ docker pull ruby:2.7-alpine
2.7-alpine: Pulling from library/ruby
4c0d98bf9879: Pull complete
e5fcc264663b: Pull complete
1c151e279a03: Pull complete
ff2cf7ba9ddf: Pull complete
773be079cf77: Pull complete
Digest: sha256:7524541d03ceacaccc8da4721681de0e246c2c3cc63b362e00785657cafebeb1
Status: Downloaded newer image for ruby:2.7-alpine
docker.io/library/ruby:2.7-alpine
Note
If no tag is specified, it will default to latest tag.
2. Pulling image from private docker registry
To pull image from private docker registry like Gitlab registry, AWS ECR etc. full path of the image needs to be provided.
Usage
$ docker pull registry.example.com/mynamespace/myproject/my/image:rc1
Example
$ docker pull registry.gitlab.com/project/image:tag
Note
Authentication to private registry might be required prior to pulling the image.
3. Starting a container in daemon mode
Usage
$ docker run -dit --name container_name image:tag
Example
$ docker run -dit --name ruby_container ruby:2.7-alpine
e2ee5f8f33c7852d9660cb3b3b6fa11fd43deeb711b5fbb49040095d5641517e
Note
If no name is provided in the --name flag, then Docker will provde some random names to the container. It is recommended to provide a name so that
the running containers can be identified easily.
4. Listing out containers
To list running container
Usage
$ docker ps
Example
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2ee5f8f33c7 ruby:2.7-alpine "irb" 3 seconds ago Up 1 second ruby_container
To list all the containers including stopped ones
Usage
$ docker ps -a
The status of the container can be seen in the STATUS column in the result.
Example
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3a599e12c9f ruby:2.7-alpine "ip a" 4 seconds ago Exited (0) 3 seconds ago ruby_test
e2ee5f8f33c7 ruby:2.7-alpine "irb" About a minute ago Up About a minute ruby_container
In this example we can see that the container ruby_test is not running and ruby_container is still running.
5. Stopping a running container
$ docker stop container_name
$ docker stop container_id
6. Deleting a container
Only stopped containers can be deleted, so it is required to stop the container before deleting.
$ docker rm container_name
OR
$ docker rm container_id
Note
container_id can be obtained from the docker ps -a command.
Verify if the container has been deleted with
$ docker ps -a
7. Listing docker images
$ docker images
8. Removing docker images
$ docker rmi image_name:tag
OR
$ docker rmi image_id
Note
- All the containers created from the image needs to be deleted before removing the image itself.
image_idcan be obtained from thedocker imagescommand.
9. Passing Environment variables to Docker during runtime
Environment variables are passed with -e flag during creation of container from a Docker image.
docker run -dit --name project-x-backend -e BUCKET_NAME=x-bucket -e USERNAME=superadmin -e DB_URI=postgresql://admin:superDBpassword@db.server.net:5432/project-x-prod-db project-x:latest
Note
If multiple parameters are required to be passed in a docker run command such as multiple environment varialbes, ports etc. then it is recommended
to use Docker Compose to manage all those with a single file.
Dockerfile References
1. Dockerfile Sample
FROM ruby:2.7-alpine
RUN apk update \
&& apk add --quiet gcc make
RUN gem update --system --quiet
RUN gem install bundler --quiet
WORKDIR /opt
COPY Gemfile Gemfile.lock /opt/
ENV BUNDLER_VERSION 2.0.1
RUN export BUNDLER_VERSION=2.0.1 && bundle install --quiet
COPY ./ /opt
RUN ln -sf /dev/stdout /opt/log/production.log
ENV ENVIRONMENT production
CMD sh -c "puma -e production"
2. Build Docker Image with Dockerfile
Usage
$ docker build -t registry.gitlab.com/project/image:tag .
$ docker build -t registry.gitlab.com/project/image:tag -f /path/to/Dockerfile
3. Pushing Docker Image to Registry
Usage
docker push registry/scope/project/image:tag
docker push registry.gitlab.com/project-x/backend:2.1
-
Contributors: Avash Mulmi ↩