I usually forget everything if I don’t use it that much. Here is a brief list of docker commands learned on the go.
Start a simple container
$ docker run -i -t ubuntu /bin/bash
-iallows you to write to the container (“Keep STDIN open even if not attached”)
-tallocates a pseudoTTY and allows to read from the container
ubuntuis the name of the image to start
/bin/bashis the command to launch once the image is started
In practice: use
-i -t to read and write from the docker image in the console.
--name XXXassigns the name XXX to the container
-dlaunches the container as a daemon
-c XXXpasses XXX to the command. In our example the command is
/bin/bash. With -c we can pass any kind of commands; for example
-c "while true; do echo hello world; sleep 1; done". Docker would pass this string t bash and the container would print hello world every second without stopping. Note the quotes.
See containers status
$ docker ps -a
psalone prints only the active containers
-ashows also the exited containers
Inspecting a container
If the container is started as daemon you might want to see the output.
$ docker logs -t -f NAME_OF_CONTAINER
logs NAME_OF_CONTAINERshows you the logs of the container. Without options you see the latest logs and then the propt gets back to your shell.
tail -f; logs become live. Use
-tshows also timestamps.
To check container processes:
$ docker top daemon_dave
To get stats about a bunch of docker containers:
$ docker stats daemon_dave daemon_kate
Running programs in a container
$ sudo docker exec -d daemon_dave touch /etc/new_config_file
-dstands for daemon mode.
execis used to run a command in the
$ docker exec -t -i daemon_dave /bin/bash
-tcreates a TTY
…basically opens an interactive shell.
Stopping & Deleting
$ docker stop CONTAINER
$ docker rm CONTAINER
Listing docker images
$ docker images