Docker Basics
Docker Basics
Introduction To Docker
Contents
Docker
1 Understanding Docker and Containerized
Applications.
Docker CLI
2 Getting started with using Docker CLI and
working with containerized applications
Dockerizing an Application
3 Dockerize your own application in a custom
docker image
2
1. Docker
4
Docker Containers
5
Microservices
6
Why Use Docker
7
Serverless
8
Clicker Quiz
Which of the following is an advantage of using Docker
Containers over Virtual Machines?
There are many publicly available images that we can use to work with Docker.
The example below pulls a hello-world image using the docker pull command:
[ ~ ]docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
13
Create A Container
To create a container from an image we can use the docker create command
2ffd5f2c5a7562fbf1d7b89a14c11a52e5843dd7938f380a8cd53f3952da99de
14
Run A Container
To run a container we can use the docker container start command to start a
container. The -i runs the container interactively and allows us to see the output
15
Run An Image
There is a shortcut for building a container from an image and running it with the
docker run command. This will create a new container for an image and run it:
16
List Images
To see what images are already installed on your machine you can use the
docker image ls command. We can see our hello-world image below:
[ ~ ]docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f63181f19b2f 13 hours ago 72.9MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
17
List Containers
To list the containers that we have built, we can use the docker container ls command.
The -a flag allows us to see both stopped and running containers. There are two containers
below, one that was built with the docker create command and the other that was built with
docker run:
[ ~ ]docker container ls –a
18
Running Interactively
Running containers interactively allows you to run commands inside the container if it
supports it. We can use the openjdk image that we used before:
19
List Running Processes
To see what containers are currently running, we can use the docker ps command. This
is useful when you are running containers in the background.
[ ~ ]docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55e4a7c3ddcc openjdk "jshell" 11 seconds ago Up 10 seconds affectionate_kowalevski
20
Interactive shell
25
Adding A Dockerfile
Dockerfile
26
Dockerfile Syntax
27
Dockerfile Explained
To build a docker image using a Dockerfile we can use the docker image build
command and provide it the directory where the Dockerfile exists. The --tag
option allows us to name and tag the docker image.
29
Run Our Custom Image
We can use the docker run command to run our image and we can see that our
tests are being run:
30
Run Interactively
We can use docker run -it to run our image interactively and open a bash
shell in our working directory:
31
Docker Compose File
32
Run Docker Compose
To run our docker compose file, we use the docker-compose up command. This
builds all images and and runs and containers.
33
Stop Containers
The calculator container will keep restarting unless its stopped. To stop all
services, we can use the docker-compose down command:
34
Clean Up
To remove all unused docker resources, we can use the docker system prune
command with the --all flag:
35
Clicker Quiz
What’s the difference between the RUN and CMD commands
in the Dockerfile?