Skip to content

Instantly share code, notes, and snippets.

@Ivlyth
Created March 17, 2016 08:03
Show Gist options
  • Save Ivlyth/b59bd8e7577e6fb85a3a to your computer and use it in GitHub Desktop.
Save Ivlyth/b59bd8e7577e6fb85a3a to your computer and use it in GitHub Desktop.
learn docker

run a image

docker run IMAGE_NAME COMMAND[s]

list running containers

docker ps

If you list the running containers, you will see that none are running.

That’s because as soon as the container did its job (echoing hello world) it stopped.

However, it is not totally gone, and you can see it with the docker ps -a command:

docker ps -a

You know how to run a container interactively but would like to run a service in the background.

Use the -d option of docker run.

docker run -d IMAGE_NAME COMMAND[s]

Creating, Starting, Stopping, and Removing Containers

$ docker create -P --expose=1234 python:2.7 python -m SimpleHTTPServer 1234 a842945e2414132011ae704b0c4a4184acc4016d199dfd4e7181c9b89092de13

$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED ... NAMES a842945e2414 python:2.7 "python -m SimpleHTT 8 seconds ago ... fervent_hodgkin $ docker start a842945e2414 a842945e2414 $ docker ps CONTAINER ID IMAGE COMMAND ... NAMES a842945e2414 python:2.7 "python -m SimpleHTT ... fervent_hodgkin

To stop a running container,

you have a choice between docker kill (which will send a SIGKILL signal to the container)

or docker stop (which will send a SIGTERM and after a grace period will send a SIGKILL).

The end result will be that the container is stopped

and is not listed in the list of running containers returned by docker ps.

However, the container has not yet disappeared (i.e., the filesystem of the container is still there);

you could restart it with docker restart or remove it forever with docker rm:

$ docker restart a842945e2414 a842945e2414 $ docker ps CONTAINER ID IMAGE COMMAND ... a842945e2414 python:2.7 "python -m SimpleHTT ... $ docker kill a842945e2414 a842945e2414 $ docker rm a842945e2414 a842945e2414 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED NAMES

Building a Docker Image with a Dockerfile

A Docker le is a text file that describes the steps that Docker needs

to take to prepare an image—including installing packages,

creating directories, and defining environment variables, among other things.

Create the following text file named Dockerfile in an empty working directory:

FROM busybox
ENV foo=bar

Then to build a new image called busybox2, you use the docker build command like so:

$ docker build -t busybox2 . Sending build context to Docker daemon 2.048 kB Step 0 : FROM busybox 24 | Chapter 1: Getting Started with Docker STATUS PORTS NAMES www.it-ebooks.info latest: Pulling from library/busybox cf2616975b4a: Pull complete 6ce2e90b0bc7: Pull complete 8c2e06607696: Pull complete Digest: sha256:df9e13f36d2d5b30c16bfbf2a6110c45ebed0bfa1ea42d357651bc6c736d5322 Status: Downloaded newer image for busybox:latest ---> 8c2e06607696 Step 1 : ENV foo bar ---> Running in f46c59e9bdd6 ---> 582bacbe7aaa

Once the build completes, you can see the new images returned by docker images

and you can launch a container based on it to check that the container has the environment variable foo set to bar:

$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE busybox2 latest 582bacbe7aaa 6 seconds ago 2.433 MB busybox latest 8c2e06607696 3 months ago 2.433 MB

$ docker run busybox2 env | grep foo foo=bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment