Docker/Basics: Difference between revisions
From charlesreid1
No edit summary |
|||
| Line 51: | Line 51: | ||
[https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only Docker run documentation] | [https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only Docker run documentation] | ||
}} | }} | ||
=Instances= | |||
==Listing all/running containers== | |||
To list containers that are currently running: | |||
<pre> | |||
$ docker ps | |||
</pre> | |||
To list all containers that have been run: | |||
<pre> | |||
$ docker ps -a | |||
</pre> | |||
If, for example, I run the docker hello world app, it will print a hello world message and then shut down: | |||
<pre> | |||
$ docker run hello-world | |||
Hello from Docker! | |||
This message shows that your installation appears to be working correctly. | |||
To generate this message, Docker took the following steps: | |||
1. The Docker client contacted the Docker daemon. | |||
2. The Docker daemon pulled the "hello-world" image from the Docker Hub. | |||
3. The Docker daemon created a new container from that image which runs the | |||
executable that produces the output you are currently reading. | |||
4. The Docker daemon streamed that output to the Docker client, which sent it | |||
to your terminal. | |||
To try something more ambitious, you can run an Ubuntu container with: | |||
$ docker run -it ubuntu bash | |||
Share images, automate workflows, and more with a free Docker ID: | |||
https://cloud.docker.com/ | |||
For more examples and ideas, visit: | |||
https://docs.docker.com/engine/userguide/ | |||
</pre> | |||
Now, if I run the ps command to list running containers, it will not show up, because it shut down when it was finished printing its message. But if I run the ps -a command, I will see when it ran and what it was called: | |||
<pre> | |||
charles @ rojo via 104.200.154.88 [ 2017-03-24 - 05:17:03 - 57 ] ~ $ docker ps | |||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
charles @ rojo via 104.200.154.88 [ 2017-03-24 - 05:18:26 - 58 ] ~ $ docker ps -a | |||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
d6a0776634d4 hello-world:latest "/hello" About a minute ago Exited (0) About a minute ago cranky_blackwell | |||
</pre> | |||
Note that <code>cranky_blackwell</code> is just an autogenerated name from docker, since we didn't specify a name. We could have used the <code>--name</code> flag: | |||
<pre> | |||
$ docker run --name my_uncreative_name hello-world | |||
Hello from Docker! | |||
This message shows that your installation appears to be working correctly. | |||
To generate this message, Docker took the following steps: | |||
1. The Docker client contacted the Docker daemon. | |||
2. The Docker daemon pulled the "hello-world" image from the Docker Hub. | |||
3. The Docker daemon created a new container from that image which runs the | |||
executable that produces the output you are currently reading. | |||
4. The Docker daemon streamed that output to the Docker client, which sent it | |||
to your terminal. | |||
To try something more ambitious, you can run an Ubuntu container with: | |||
$ docker run -it ubuntu bash | |||
Share images, automate workflows, and more with a free Docker ID: | |||
https://cloud.docker.com/ | |||
For more examples and ideas, visit: | |||
https://docs.docker.com/engine/userguide/ | |||
$ docker ps -a | |||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
48d33ae27050 hello-world:latest "/hello" 31 seconds ago Exited (0) 30 seconds ago my_uncreative_name | |||
d6a0776634d4 hello-world:latest "/hello" 3 minutes ago Exited (0) 3 minutes ago cranky_blackwell | |||
</pre> | |||
[[Category:Docker]] | [[Category:Docker]] | ||
Revision as of 05:20, 24 March 2017
Filesystem basics
Working directory
You can set the working directory using the -w flag:
$ docker run -w /path/to/dir/ -i -t ubuntu pwd
Volumes
You can set storage drive options using storage opt fllag:
$ docker run -it --storage-opt size=120G fedora /bin/bash
You can also mount external (your machine) drives/folders inside the Docker container:
$ docker run \ -v `pwd`:`pwd` \ -w `pwd` \ -i -t ubuntu pwd
The -v flag mounts the current working directory into the container. The -w lets the command being executed inside the current working directory, by changing into the directory to the value returned by pwd. So this combination executes the command using the container, but inside the current working directory.
Docker creates any folders on your machine if they don't exist, before it spins up the docker container:
$ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash
Read only control can be set on volumes as well: --read-only flag.
$ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh
The--read-onlyflag mounts the container’s root filesystem as read only prohibiting writes to locations other than the specified volumes for the container.
Instances
Listing all/running containers
To list containers that are currently running:
$ docker ps
To list all containers that have been run:
$ docker ps -a
If, for example, I run the docker hello world app, it will print a hello world message and then shut down:
$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
Now, if I run the ps command to list running containers, it will not show up, because it shut down when it was finished printing its message. But if I run the ps -a command, I will see when it ran and what it was called:
charles @ rojo via 104.200.154.88 [ 2017-03-24 - 05:17:03 - 57 ] ~ $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES charles @ rojo via 104.200.154.88 [ 2017-03-24 - 05:18:26 - 58 ] ~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6a0776634d4 hello-world:latest "/hello" About a minute ago Exited (0) About a minute ago cranky_blackwell
Note that cranky_blackwell is just an autogenerated name from docker, since we didn't specify a name. We could have used the --name flag:
$ docker run --name my_uncreative_name hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48d33ae27050 hello-world:latest "/hello" 31 seconds ago Exited (0) 30 seconds ago my_uncreative_name
d6a0776634d4 hello-world:latest "/hello" 3 minutes ago Exited (0) 3 minutes ago cranky_blackwell