-
Notifications
You must be signed in to change notification settings - Fork 3
5 Docker Commands to know
Now that you have docker set up on your computer there will be a few commands that you'll need to know.
While not strictly a docker command, we do use docker-compose as our primary way of managing the actual running of docker. See Docker Compose on how to set up a docker-compose file.
The command docker-compose up
will run the file named docker-compose.yaml
and start up any services specified there. This will run in your current terminal,
allowing you to ^C the running services but blocking you from using that window.
If you aren't using something like tmux then you
likely will want to add the -d
flag, which "detaches" the process from the
current terminal's STDOUT. To stop the processes when using -d
run docker-compose down
.
If you want to use a different file then add the
-f alternate-file.yaml
flag along with the different file you want to use.
An example usage of the command that shows both flags from the clonm/docker-openldap is:
docker-compose up -f docker-rocketchat.yaml -d
Much of the docker philosophy can be summed up in this command. You don't want to build a working system that does something, you want to build a system that builds systems. To that end you won't want old versions of what you're building to stick around, it turns out to be a lot faster to burn everything down and rebuild.
You want to run docker container prune
every time you want to reset a system.
In general it is safe to run every time you want to bring containers down and
take them back up. It won't kill currently running containers.
Example usage:
docker container prune
Docker networks much like containers ought to be built automatically. Just like container prune, network prune removes docker networks.
If you are moving from fixing one set of containers to another (e.g. cloyne/docker-rocketchat to clonm/docker-openldap) and you get some odd form of network error, bring all containers down and run a prune. That should fix things up.
Example usage:
docker network prune
When you make a change to the Dockerfile a container is based on for testing you will need to build it locally to see the effects. This is especially important if you are installing a new package, or changing major environment variables.
The -t
flag allows you to specify a "tag" to give the newly built container.
You will usually need to do this with docker-compose, as it will try to pull
tagged containers matching the name it is provided unless one with the correct
tag is already on the system.
On a unix based system the '/dir/to/build' will usually be '.' the current directory if you are editing the Dockerfile from the directory it resides in.
An example of building the Cloyne's Rocket.Chat service for local testing is:
docker build -t cloyne/rocketchat .
Only now that you've made it to the end of the list do you actually find out how
to tell a container to do something. This isn't an accident. Whenever there is a
command that should be run on a container for it to function, that should be
in the Dockerfile or the docker-compose.yaml file. docker exec
should be used
to test what it is you need to put in one of those files, and to diagnose what
is going wrong in a system when logs don't show you.
The -t
flag allocates a pseudo-TTY (think:thing that a shell runs in). The
-i
flag is for "interactive" mode, keeping STDIN open even while not actively
writing to it. You usually will use both of these together to you can run a login
shell (usually bash). --user
allows you to specify the user to run a command
as, usually root.
An example of using exec to open an interactive shell for rocketchat is:
docker exec --user root -ti rocketchat /bin/bash --login
If you need to know more on anything docker related consult the man pages.
Man (short for manual) has detailed, albeit terse explanations of commands.
For example to see more on how docker network prune
works run:
man docker-network-prune
Similarly to learn more on docker exec
:
man docker-exec
There is always more to learn when it comes to tech. These commands should help you get started, but always keep learning more.