docker
then followed by a
commanddocker --help
docker version
docker system info
docker <command> [options]
docker <command> <subcommand> [options]
-
(single dash)--
(double dash)docker version
Client:
Version: 27.2.0
API version: 1.47
Go version: go1.21.13
Git commit: 3ab4256
Built: Tue Aug 27 14:17:17 2024
OS/Arch: windows/amd64
Context: desktop-linux
Server: Docker Desktop 4.34.0 (165256)
Engine:
Version: 27.2.0
API version: 1.47 (minimum version 1.24)
Go version: go1.21.13
Git commit: 3ab5c7d
Built: Tue Aug 27 14:15:15 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.20
GitCommit: 8fc6bcff51318944179630522a095cc9dbf9f353
runc:
Version: 1.1.13
GitCommit: v1.1.13-0-g58aa920
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Recent change in Docker Hub, may require to authenticate your Docker Hub's account:
docker login --username your_username
Password:
Login Succeeded
Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
Program (binary/script)
Process
Image (specific format)
Container (isolated)
create
nginx
We will be using the following application as an example application:
nginx
- https://nginx.org
Create a new container.
docker container create --publish 8000:80 nginx:alpine
container
- management command related to
containercreate
- subcommand to create a new container--publish 8000:80
- use host port 8000 to access port
80 in containernginx:alpine
- image named nginx
with tag
alpine
docker container create --publish 8000:80 nginx:alpine
docker
will:
nginx
with tag
alpine
Unable to find image 'nginx:alpine' locally
alpine: Pulling from library/nginx
213ec9aee27d: Already exists
ae98275d0ecb: Pull complete
121e2d9f6af2: Pull complete
6a07d505af0f: Pull complete
3e8957b70867: Pull complete
2806408d582e: Pull complete
Digest: sha256:b433a017703c4a866c44620ed97f603555dee677756ae24df13a4329276fc0fd
Status: Downloaded newer image for nginx:alpine
4fb1731e95101606b28c5331a049501d162476287baa726c3aad23152b4298f8
The last line is the container ID of the created container.
Check the created container:
docker container list --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 4 minutes ago Created friendly_haslett
list
- subcommand to list containers--all
- list all containers (default shows only running
containers)Alternate command:
docker container ps -a
start
Run the created container:
docker container start 4fb1731e9510
Explaination:
start
- subcommand to start one or more stopped
containers
4fb1731e9510
- the container ID to
run
4f
Check the started container:
docker container list --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 10 minutes ago Up 3 seconds 0.0.0.0:8000->80/tcp friendly_haslett
Test nginx
container with browser using port
8000 on docker host
Test nginx
container with command line
curl
using port 8000 on docker
hostcurl --head http://localhost:8000
HTTP/1.1 200 OK
Server: nginx/1.23.2
Date: Thu, 27 Oct 2022 07:15:04 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 19 Oct 2022 10:28:53 GMT
Connection: keep-alive
ETag: "634fd165-267"
Accept-Ranges: bytes
run
Use run
subcommand to run another instance of
nginx
docker container run --publish 8001:80 --detach nginx:alpine
run
- subcommand to run a new container--publish 8001:80
- use host port 8001 to access port
80 in container--detach
- run container in background and print
container IDUse ps
(can also use ls
, list
)
to see current status of containers.
docker container ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b304f8c5f78 nginx:alpine "/docker-entrypoint.…" 28 minutes ago Up 28 minutes 0.0.0.0:8001->80/tcp upbeat_lamarr
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 3 hours ago Up 2 hours 0.0.0.0:8000->80/tcp friendly_haslett
Run program sh
in nginx
image
interactively.
docker container run --interactive --tty nginx:alpine sh
--interactive
- run container and keep STDIN open even
if not attached--tty
- allocate a pseudo-TTYAlternate command:
docker container run -it nginx:alpine sh
sh
programWhen running a sh
program:
exit
Ctrl+D
To check status of all containers:
docker container ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
258c620834e1 nginx:alpine "/docker-entrypoint.…" 7 minutes ago Exited (0) 7 minutes ago sleepy_mahavira
1b304f8c5f78 nginx:alpine "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8001->80/tcp upbeat_lamarr
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 4 hours ago Up 3 hours 0.0.0.0:8000->80/tcp friendly_haslett
To check resources used by running containers:
docker container stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
1b304f8c5f78 upbeat_lamarr 0.00% 6.371MiB / 24.76GiB 0.03% 1.22kB / 0B 0B / 0B 9
4fb1731e9510 friendly_haslett 0.00% 6.488MiB / 24.76GiB 0.03% 7.83kB / 7.52kB 0B / 0B 9
To exit, type Ctrl+C
To check all process running in a container:
docker container top 4fb1731e9510
UID PID PPID C STIME TTY TIME CMD
root 3328 3308 0 06:18 ? 00:00:00 nginx: master process nginx -g daemon off;
uuidd 3376 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3377 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3378 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3379 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3380 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3381 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3382 3328 0 06:18 ? 00:00:00 nginx: worker process
uuidd 3383 3328 0 06:18 ? 00:00:00 nginx: worker process
stop
To stop a running container:
docker container stop 4fb1731e9510
stop
- stop one or more running containers4fb1731e9510
- the container ID to
stopCheck status of the stopped container
docker container ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
258c620834e1 nginx:alpine "/docker-entrypoint.…" 18 minutes ago Exited (0) 7 minutes ago 80/tcp sleepy_mahavira
1b304f8c5f78 nginx:alpine "/docker-entrypoint.…" 2 hours ago Up 2 hours ago 0.0.0.0:8001->80/tcp upbeat_lamarr
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 4 hours ago Exited (0) 19 seconds ago friendly_haslett
rm
To remove a container
docker container rm 258c620834e1
rm
- remove one or more containers258c620834e1
- the container ID to
removeNote:
Check status of the removed container
docker container ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b304f8c5f78 nginx:alpine "/docker-entrypoint.…" 2 hours ago Up 2 hours ago 0.0.0.0:8001->80/tcp upbeat_lamarr
4fb1731e9510 nginx:alpine "/docker-entrypoint.…" 4 hours ago 2 minutes ago friendly_haslett
You can use pre-built images from image registry.
Normally you do not need to download docker image manually.
Dockerfile
To create your own image for your application
Dockerfile
A Dockerfile
is a text file that
contains:
The format of a Dockerfile
nginx
Dockerfile
Simple custom image using nginx
Explaination:
FROM
- instruction to use the following image as base
imagedocker image build --tag my_nginx:1 .
Explaination:
image
- management command to manage imagesbuild
- subcommand to build an image from a
Dockerfile
--tag
- name and optionally a tag in the
'name:tag' formatmy_nginx:1
- name the repository of this image as
my_nginx
with tag 1
.
- path to context (where Dockerfile
is)
to be used during image building processTo list local images on docker host:
docker image ls
Explaination:
ls
- subcommand to list imagesTo list image from specifc repository:
docker image ls my_nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
my_nginx 1 ad9ab5e40cef 8 days ago 23.6MB
Run a new container from the built image.
docker container run --detach --publish 8001:80 --name n1 my_nginx:1
Explaination:
--detach
- run container in background and print
container ID--publish 8001:80
- use host port 8001
to
access port 80
in container--name
- assign a name n1
to the
containermy_nginx:1
- use local image with repository
my_nginx
with tag 1
Test the running container.
curl http://localhost:8001
nginx
Dockerfile
Simple custom image using nginx
: - add a web page
Explaination:
FROM
- instruction to use the following image as base
imageCOPY
- copy file page.html
(relative to
context path) to path in imagedocker image build --tag my_nginx:2 .
Explaination:
image
- management command to manage imagesbuild
- subcommand to build an image from a
Dockerfile
--tag
- name and optionally a tag in the
'name:tag' formatmy_nginx:2
- name the repository of this image as
my_nginx
with tag 2
.
- path to context (where Dockerfile
is)
to be used during image building processTo list local images on docker host:
docker image ls
Explaination:
ls
- subcommand to list imagesTo list image from specifc repository:
docker image ls my_nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
my_nginx 2 8b10604a4e5d 2 minutes ago 23.6MB
my_nginx 1 ad9ab5e40cef 8 days ago 23.6MB
Run a new container from the built image.
docker container run --detach --publish 8002:80 --name n2 my_nginx:2
Explaination:
--detach
- run container in background and print
container ID--publish 8002:80
- use host port 8002
to
access port 80
in container--name
- assign a name n2
to the
containermy_nginx:2
- use local image with repository
my_nginx
with tag 2
Test the running container.
curl http://localhost:8002/page.html
nginx
Run an nginx
container.
docker container run -d -p 9000:80 --name nn nginx:alpine
Explaination:
-d
- run container in background and print container
ID-p 9000:80
- use host port 9000
to access
port 80
in container--name nn
- assign a name nn
to the
containerCopy the default nginx
configuration file from the
running container.
docker container cp nn:/etc/nginx/conf.d/default.conf .
Explaination:
cp
- copy files/folders between a container and the
local filesystemModify the copied default.conf
/app
#...
root /usr/share/nginx/html/;
index index.html index.htm;
location / {
}
location /app {
root /;
}
#...
}
Dockerfile
Customise configuration of application nginx
:
FROM nginx:1.23.2-alpine
RUN mkdir -p /app
COPY default.conf /etc/nginx/conf.d/
COPY page.html /app/
Explaination:
RUN
- instruction to run any commands in a new layer on
top of the current image and commit the results. The resulting committed
image will be used for the next step in the Dockerfile.docker image build --tag my_nginx:3 .
Explaination:
image
- management command to manage imagesbuild
- subcommand to build an image from a
Dockerfile
--tag
- name and optionally a tag in the
'name:tag' formatmy_nginx:3
- name the repository of this image as
my_nginx
with tag 3
.
- path to context (where Dockerfile
is)
to be used during image building processTo list local images on docker host:
docker image ls
To list image from specifc repository:
docker image ls my_nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
my_nginx 3 568069eab905 3 minutes ago 23.6MB
my_nginx 2 8b10604a4e5d 45 hours ago 23.6MB
my_nginx 1 ad9ab5e40cef 8 days ago 23.6MB
Run a new container from the built image.
docker container run --detach --publish 8003:80 --name n3 my_nginx:3
Explaination:
--detach
- run container in background and print
container ID--publish 8003:80
- use host port 8003
to
access port 80
in container--name n3
- assign a name n3
to the
containermy_nginx:3
- use local image with repository
my_nginx
with tag 3
Test the running container.
curl http://localhost:8003/app/page.html
Remove containers using their container names
docker container rm n1 n2 n3
python
main.py
Create a simple python file main.py
with the following
contents:
Dockerfile
FROM python:3.11-alpine
WORKDIR /app
COPY main.py /app/
# exec form
CMD ["python", "main.py"]
# shell form
#CMD python3 main.py
Explaination:
WORKDIR
- instruction to set the working directory for
any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow itCMD
- instruction to provide defaults for an executing
containerdocker image build --tag my_app_python:1 .
Explaination:
image
- management command to manage imagesbuild
- subcommand to build an image from a
Dockerfile
--tag
- name and optionally a tag in the
'name:tag' formatmy_python_app:1
- name the repository of this image as
my_python_app
with tag 1
.
- path to context (where Dockerfile
is)
to be used during image building processTo list local images on docker host:
docker image ls
To list image from specifc repository:
docker image ls my_app_python
REPOSITORY TAG IMAGE ID CREATED SIZE
my_app_python 1 297f2a03733c 2 hours ago 51.1MB
Run a new container from the built image.
docker container run --interactive --tty --name p1 my_app_python:1
Explaination:
--interactiveh
- keep STDIN open even if not
attached--tty
- allocate a pseudo-TTY--name p1
- assign a name p1
to the
containermy_app_python:1
- use local image with repository
my_app_python
with tag 1
Remove container using its container name.
docker container rm p1
java
Main.java
Create a simple java file Main.java
with the following
contents:
import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String line = sc.nextLine();
sc.close();
List<String> digits = new ArrayList<>();
List<String> chars = new ArrayList<>();
for (char ch : line.toCharArray()) {
if (Character.isDigit(ch)) {
digits.add(Character.toString(ch));
} else if (Character.isLetter(ch)) {
chars.add(Character.toString(ch));
}
}
String s1 = String.join(" ", digits);
String s2 = String.join(" ", chars);
System.out.println("digits: " + s1);
System.out.println("chars: " + s2);
}
}
Dockerfile
FROM amazoncorretto:8-alpine-jdk as base
WORKDIR /app
COPY Main.java /app/
RUN javac Main.java
FROM amazoncorretto:8-alpine-jre as production
WORKDIR /app
COPY --from=base /app/Main.class /app/
# exec form
CMD ["java", "Main"]
# shell form
#CMD java Main
Note: Use multi-stage build
docker image build --tag my_app_java:1 .
Explaination:
image
- management command to manage imagesbuild
- subcommand to build an image from a
Dockerfile
--tag
- name and optionally a tag in the
'name:tag' formatmy_app_java:1
- name the repository of this image as
my_app_java
with tag 1
.
- path to context (where Dockerfile
is)
to be used during image building processTo list local images on docker host:
docker image ls
To list image from specifc repository:
docker image ls my_app_java
REPOSITORY TAG IMAGE ID CREATED SIZE
my_app_java 1 62fe67987d14 48 minutes ago 110MB
Run a new container from the built image.
docker container run --interactive --tty --name j1 my_app_java:1
Explaination:
--interactive
- keep STDIN open even if not
attached--tty
- allocate a pseudo-TTY--name j1
- assign a name j1
to the
containermy_app_java:1
- use local image with repository
my_app_java
with tag 1
Remove container using its container name.
docker container rm j1