Docker has revolutionized the way software is being written and deployed throughout our industry. With the use of it’s lightweight containers you can deploy your whole environment locally – or in your clients machine quickly and easily.
At Practical Software we’ve used Docker to test how multiple servers behaves when one of them crash and burn and it became extremely handy when we’ve needed to test code on a specific Linux distribution and din’t have a lot of time to install it from scratch.
When I’ve first started to learn about Docker (and then orchestration) it seemed to me that the more I learn the more I know how much I don’t understand when in fact what I’ve needed to do is just try it out.
And so I bring before you my list of commands that helped me start using docker:
1. docker run
Right after you’ve installed docker on your machine you would want to try it out, head over to DockerHub and find a container you want to use. it could be a database or service you use.
and once you find your image of choice just write:
docker run <container name>
And you would get it up and running, if you’ll go to that container page ther’s a good chance you can find additional parameters you can use to determine how the container behaves and you would probably want to expose one or more ports to the outside world as well.
So if you you’ve wanted to start a new container running PostgreSQL this is all you need to run:
docker run –e POSTGRES_USER=dror –p 5432:5432 postgres:latest
-e is for passing parameters, in this case the DB admin user name
-p exposes the containers port 5432 as my machine’s port so I can reach it using localhost:5432
2. docker ps
Once you have a few containers up and running you might want to see how are they doing or take a quick inventory of what you have and for that you have the ps command
Just run docker ps to see a list of running containers or docker ps -a to show all of the containers.
3. docker stop
As the name implies it stops a running container if for example I would have written
docker stop 552470331f33
Then the first container would have stopped running.
You can combine docker stop and docker ps to stop all of the running containers
docker stop $(docker ps -q)
Written in PowerShell will get a list of all of the running containers ids (this is what the -q is for) and stop them
4. docker logs
docker log is used to show you the output and errors written by applications running inside your containers. Especially useful if (read: when) they crash. You can see the logs from the past or you can use -f just like good old tail to see it in real time.
docker system prune
After you’ll use docker for a while things then to pile up: old container images, unused networks and quite a lot of junk. to remove all of this unused data run docker system prune and boldly approve when docker insists on checking if you really, really know what you’re doing.
That’s it for now, obviously there’s still quite a lot to learn in order to use docker in production but the first step of installing docker and getting your hands dirty is simple enough.
Once you finish running the basic commands, find a good book, a blog post or a fellow developer and you’ll never look back. And if your in Israel next week – you might want to check out our Docker for Developers course where Dror G and myself will talk about everything a developer needs to know to harness the power of docker.
And until then – happy coding…
One thought on “5 commands to get you up and running with Docker”
docker system prone
That should be 5. docker system prune