Docker
Numan Shafi
Cloud Computing (Fall 2023)
UET Lahore, Pakistan
Install Docker Engine on Ubuntu
● You need 64-bit version of one of these Ubuntu versions:
○ Ubuntu Kinetic 22.10
○ Ubuntu Jammy 22.04 (LTS)
○ Ubuntu Focal 20.04 (LTS)
○ Ubuntu Bionic 18.04 (LTS)
● Check your Ubuntu Release and Code Name using:
○ cat /etc/*release*
● Install using the convenience script (Click the link)
● The following two commands will install the docker on your Ubuntu machine
curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sudo sh ./get-docker.sh
Install Docker Engine on Ubuntu (Cont.)
● If the installation works well, you see the following output:
Docker Commands
● docker run is a command used in the Docker command-line interface (CLI) to
run a command in a new container.
● OPTIONS are various options that can be passed to the command, such as specifying ports
to publish, setting environment variables, and mounting volumes.
● IMAGE is the image you want to use to create the container.
● COMMAND is the command you want to run inside the container.
● ARG are any arguments to be passed to the command.
Docker Commands (Cont.)
● docker ps is a command used in the Docker command-line interface (CLI) to
list all running containers on a system.
● The docker ps -a command lists all containers, both running and stopped, on
a system.
● Docker stop [NAME/ID of Container] stops the running container
Docker Commands (Cont.)
● Docker start [NAME/ID of Container] starts the stopped container. It will
restore the file system for the container too.
● docker rm is a command used in the Docker command-line interface (CLI) to
remove one or more containers.
Docker Commands (Cont.)
Docker Commands (Cont.)
● docker images will list local images, their repository, tags, and their size.
● docker rmi removes images from the host node.
Docker Commands (Cont.)
● docker run downloads image
from docker registry locally
and then run it.
● docker pull only downloads
image from docker
registry locally.
Docker Commands (Cont.)
● docker exec is a command used to run a command inside an existing running container.
● -i option stands for interactive, this allows you to interact with
the container
● -t option allows you to have a pseudo-terminal
● container_name is the name or ID of the container to
connect to
● command is the command you want to run inside the
container
Docker Commands (Cont.)
What happened here?
Docker Commands (Cont.)
● Docker by default run some command inside. If there is no command defined the
container terminates right after running
● For example docker run ubuntu will run ubuntu container but terminate as we do not
define any command and there is no default command for this image
Docker Commands (Cont.)
The docker inspect command is used to get detailed information about Docker objects such as
images, containers, networks, and volumes. By default it returns data in the form of a JSON array.
Docker Commands (Cont.)
The docker logs command
shows information logged by a
running container.
Docker Commands (Cont.)
Tag
s
● Docker Images are
managed by tags.
● The default tag for every
image is latest. However,
we can also use specific
tags.
● Tags are mostly used to
refer a specific version.
Docker Hub
Volume Mapping
Once we remove/delete docker, the file system of the docker also destroyed. We
can map a local directory to the container for avoid it.
● docker run –v /opt/datadir:/var/lib/mysql mysql
○ /opt/datadir is local directory in the host node
○ /var/lib/mysql is directory in the container
Docker Environment Variable
Docker Environment Variable (Cont.)
Docker Environment Variable (Cont.)
Docker Environment Variable (Cont.)
Building Custom Docker Images
● A Dockerfile is a script that contains instructions for building a Docker image.
● It is used to automate the process of creating a container image.
● It typically includes information about the base image, any additional software
that needs to be installed, and any configurations or environment variables that
need to be set.
● The basic syntax for Dockerfile is:
INSTRUCTION arguments
Dockerfile
The most commonly used instructions in Dockerfile are:
● FROM: specifies the base image for the build
● RUN: runs a command to install software or make other changes to the image
● COPY: copies files or directories from the host machine to the image
● ENV: sets environment variables
● EXPOSE: specifies the ports that the container will listen on
● CMD: specifies the command that will be run when a container is started from the image
● ENTRYPOINT: instruction sets the command that will be executed when the container is started from the image.
Unlike the CMD instruction, the ENTRYPOINT instruction does not get overridden when additional command-line arguments
are passed to the docker run command
Dockerfile (Cont.)
Dockerfile (Cont.)
Docker Compose
● Docker Compose is a tool for defining and running multi-container Docker applications.
● It is used to define the services, networks and volumes for the application, and then
starts and stops all of the containers with a single command.
● The configuration for the application is defined in a docker-compose.yml file, which
specifies the services, their dependencies, and their configuration.
● This allows developers to easily manage and scale their applications, and makes it
easy to run the application in different environments.
● It also provides a way to run multiple container applications together, by grouping all
the services in one compose file.
Docker Compose (Cont.)
Docker Compose (Cont.)
Docker Compose (Cont.)
Docker Compose (Cont.)
● The --link option in docker run
allows you to link one container to
another. Syntax:
docker run --link <name/id of container to link>:<alias
in the running container> <image name>
● --link redis:redis in the example is
linking voting-app with redis
container whereas redis is also
used alias in voting-app to access
the redis container.
Docker Compose (Cont.)
Docker Compose (Cont.)
● First time, you need to install docker-compose. You can do using the following two commands:
○ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o
/usr/local/bin/docker-compose
○ sudo chmod +x /usr/local/bin/docker-compose
Docker Compose (Cont.)
If the image is not in
the registry. We can
instruct docker
compose to build the
image from a local
folder which contain
source code of the
app and Dockerfile
Docker Compose (Cont.)
The latest is version 3.
All the features of version
2 are available in it. We
do not need to specify
links and it can support
multiple networks. We
mostly use version 2.
Docker Compose (Cont.)
Credit
Part of slides are taken from KodeKloud!