This is a crosspost from . This might take a while if you don't have it yet! Subsequent runs will be faster because Docker is quite good at caching steps that do not change.
If the build was successful, you should be able to see it when you list all images with docker images.
$ docker images
REPOSITORY TAG IMAGE ID CREATED
hello-world-go latest <image-id> <timestamp>
If you don't see the image, please let me know and I will help you out :)
Great job! You just built your first docker image! 🎉
Now that we have our image, we can run it with the docker run command.
$ docker run hello-world-go
Hello, World!
This will start a new container from our image and execute the main executable, because that is what we defined with the CMD instruction in our Dockerfile.
As you can see, the container exits immediately because that is what our application does.
If you run docker ps --all, you should see that the container exited immediately.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
<container-id> hello-world-go "/main" <timestamp> Exited (0)
Make sure to run docker ps --all and not docker ps because docker ps only shows running containers by default. The --all flag is used to show all containers, including the ones that have already exited.
Now its your turn to play around with this small setup! Change some lines in the Dockerfile and see what happens. Here are some ideas:
- Change the
CMDinstruction toRUN go run main.goand see what happens. Does it still work? Why? - Change the
RUNinstruction toRUN go build -o main main.go, why does it fail? - Add a new
COPYinstruction to copy some other files into the image, does it work? - Checkout the
WORKDIRinstruction and try to use it in your Dockerfile.
If you get stuck, feel free to ask me for help! You can reach me on or by email
Anyway, that's it for today. Tomorrow we will take a deeper look at what exactly happend when we built our image and learn some more ways to interact with containers. If you want to have a headstart, checkout the docker exec command and what you can do with it!
Until then, happy hacking! 🚀
Jonas
SOCIAL SHARE CARD GENERATOR