Goal
Learn more in container.
* alpine
* nginx
* run a command
* interactive mode
* attach mode
* detach mode
alpine image
Alpine Linux is a Linux distribution built around musl libc and BusyBox.
The image is only 5 MB in size and has access to a package repository that is much more complete than other BusyBox based images.
This makes Alpine Linux a great image base for utilities and even production applications.
Run alpine container
Ex:
$ docker container run alpine
Result:
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
2408cc74d12b: Pull complete
Digest: sha256:686d8c9dfa...
Status: Downloaded newer image for alpine:latest
Then let's check the container status.
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND CREATED
11ce1d174267 alpine "/bin/sh" 34 seconds ago
STATUS PORTS NAMES
Exited (0) 33 seconds ago flamboyant_hodgkin
The alpine container exited since it had done its job (it is '/bin/sh' in this case without any output).
How to run a command?
Can we run a command in alpine container?
Let's get more info of container run command.
Ex:
$ docker container run --help
Result:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
...
The helper message shows that the docker run command supports the docker client to pass a command to run.
Ex:
$ docker container run alpine echo "Hello World"
Result:
Hello World
Lets check the container status again.
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND
aa2fe1142e60 alpine "echo 'Hello World'"
CREATED STATUS PORTS NAMES
33 seconds ago Exited (0) 32 seconds ago inspiring_cerf
Then we can see alpine container exited after alpine container executed "echo 'Hello World'".
Interactive Mode
Can we run more commands in alpine container?
Let's check the helper message again.
Ex:
$ docker container run --help
Result:
Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
The flag -it attaches us to an interactive tty in the container. Then you can run as many commands as you like.
Ex:
$ docker container run -it alpine /bin/sh
Result:
#
Before calling 'exit' during interactive mode if we check the container status by other command line tool, then you can see this container is up.
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND
c394a45cd959 alpine "/bin/sh"
CREATED STATUS PORTS NAMES
9 seconds ago Up 7 seconds silly_galois
nginx image
Nginx (pronounced "engine-x") is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server)
Run nginx container
Ex:
$ docker container run nginx
Result:
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
42c077c10790: Pull complete
62c70f376f6a: Pull complete
915cc9bd79c2: Pull complete
75a963e94de0: Pull complete
7b1fab684d70: Pull complete
db24d06d5af4: Pull complete
Digest: sha256:6860de7bf5f04336790fd390019a...
Status: Downloaded newer image for nginx:latest
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty,
will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/...
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of ...
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in ..
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-...
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-...
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/05/28 12:35:36 [notice] 1#1: using the "epoll" event method
2022/05/28 12:35:36 [notice] 1#1: nginx/1.21.6
2022/05/28 12:35:36 [notice] 1#1: built by gcc 10.2.1 20210110 ...
2022/05/28 12:35:36 [notice] 1#1: OS: Linux 5.15.0-33-generic
2022/05/28 12:35:36 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:104...
2022/05/28 12:35:36 [notice] 1#1: start worker processes
2022/05/28 12:35:36 [notice] 1#1: start worker process 31
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND
ee4a4fab47e3 nginx "/docker-entrypoint.…"
CREATED STATUS PORTS NAMES
31 seconds ago Up 29 seconds 80/tcp thirsty_lalande
Unlike alpine container, nginx container will keep running since it has not finished its job (/docker-entrypoint.sh).
And by default the container output will attach to our command line tool.
It is called attach mode, and we we can use ctrl-c to exit it.
Ex:
$ // Ctrl-C (Using ubuntu in this exp)
Result:
^C2022/05/28 12:43:14 [notice] 1#1: signal 2 (SIGINT) received, exiting
2022/05/28 12:43:14 [notice] 31#31: exiting
2022/05/28 12:43:14 [notice] 31#31: exit
2022/05/28 12:43:14 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2022/05/28 12:43:14 [notice] 1#1: worker process 31 exited with code 0
2022/05/28 12:43:14 [notice] 1#1: exit
Check the container status.
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND
ee4a4fab47e3 nginx "/docker-entrypoint.…"
CREATED STATUS PORTS NAMES
8 minutes ago Exited (0) About a minute ago thirsty_lalande
Detach Mode
Since we might want to take the prompt control back for further tasks, is there a way to detach the output?
Let's see helper message.
Ex:
$ docker container run --help
Result:
Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
-d, --detach Run container in background and print container ID
So we can run container with -d as following.
Ex:
$ docker container run -d nginx
Result:
bbd1bdce7c4177208b61a53c6fe67aad6e4f1c7bdf838913c9cf40e8601b7096
Ex:
$ docker container ls -a
Result:
CONTAINER ID IMAGE COMMAND
bbd1bdce7c41 nginx "/docker-entrypoint.…"
CREATED STATUS PORTS NAMES
11 seconds ago Up 10 seconds 80/tcp dreamy_engelbart
Also, we can use the following command to attach the running container output to our command line again.
Ex:
$ docker container attach bbd
Interactive Mode with running containers
Ex:
$ docker exec --help
Result:
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
Then we can use the following command to run /bin/sh in interactive mode.
Ex:
$ docker exec -it bbd /bin/sh
Result:
#
No comments:
Post a Comment