In Docker, everything is based on Images. An image is a combination of a file system and parameters. Let’s take an example of the following command in Docker. Docker run hello-world The Docker command is specific and tells the Docker program on the Operating System that something needs to be done. Docker history iis IMAGE CREATED CREATED BY SIZE COMMENT f4caf476e909 16 seconds ago cmd /S /C REM (nop) CMD 'cmd' 41.84 kB f0e017e5b088 21 seconds ago cmd /S /C echo 'Hello World - Dockerfile' c 6.816 MB 88438e174b7c About a minute ago cmd /S /C dism /online /enable-feature /all / 162.7 MB 6801d964fda5 4 months ago 0 B.
- Download Hello World Docker Image Size
- Download Hello World Docker Image File
- Docker Hello World Image Download
So it turns out that the Moby Project has a shell script on the Moby GitHub account which can download images from Docker Hub in a format that can be imported into Docker: download-frozen-image-v2.sh; The usage syntax for the script is given by the following: download-frozen-image-v2.sh targetdir image:tag@digest. To find out more about a Docker image, run docker image inspect alpine. In the demo above, you used the docker image pull command to download the alpine image. When you executed the command docker container run hello-world, it also did a docker image pull behind the scenes to download the hello-world image.
Mkdir hello–world cd hello–world. Create and edit a new YAML configuration file: sudo nano docker-compose.yml. YAML is a type of configuration file. This file will create a container named test-file based on the Hello World image on Docker Hub. Enter the following text into the editor.
- Docker Tutorial
- Docker Useful Resources
- Selected Reading
In Docker, everything is based on Images. An image is a combination of a file system and parameters. Let’s take an example of the following command in Docker.
The Docker command is specific and tells the Docker program on the Operating System that something needs to be done.
The run command is used to mention that we want to create an instance of an image, which is then called a container.
Finally, 'hello-world' represents the image from which the container is made.
Now let’s look at how we can use the CentOS image available in Docker Hub to run CentOS on our Ubuntu machine. We can do this by executing the following command on our Ubuntu machine −
Note the following points about the above sudo command −
We are using the sudo command to ensure that it runs with root access.
Here, centos is the name of the image we want to download from Docker Hub and install on our Ubuntu machine.
─it is used to mention that we want to run in interactive mode.
/bin/bash is used to run the bash shell once CentOS is up and running.
Displaying Docker Images
To see the list of Docker images on the system, you can issue the following command.
This command is used to display all the images currently installed on the system.
Syntax
Options
None
Return Value
The output will provide the list of images on the system.
Example
Output
When we run the above command, it will produce the following result −
From the above output, you can see that the server has three images: centos, newcentos, and jenkins. Each image has the following attributes −
TAG − This is used to logically tag images.
Image ID − This is used to uniquely identify the image.
Created − The number of days since the image was created.
Virtual Size − The size of the image.
Downloading Docker Images
Images can be downloaded from Docker Hub using the Docker run command. Let’s see in detail how we can do this.
Syntax
The following syntax is used to run a command in a Docker container.
Options
Image − This is the name of the image which is used to run the container.
Return Value
The output will run the command in the desired container.
Example
This command will download the centos image, if it is not already present, and run the OS as a container.
Output
When we run the above command, we will get the following result −
You will now see the CentOS Docker image downloaded. Now, if we run the Docker images command to see the list of images on the system, we should be able to see the centos image as well.
Removing Docker Images
The Docker images on the system can be removed via the docker rmi command. Let’s look at this command in more detail.
This command is used to remove Docker images.
Syntax
Options
Paragon ntfs for mac 64 bit windows 10. ImageID − This is the ID of the image which needs to be removed.
Return Value
The output will provide the Image ID of the deleted Image.
Example
Here, 7a86f8ffcb25 is the Image ID of the newcentos image.
Output
When we run the above command, it will produce the following result −
Let’s see some more Docker commands on images.
docker images -q
This command is used to return only the Image ID’s of the images.
Syntax
Options
q − It tells the Docker command to return the Image ID’s only.
Return Value
The output will show only the Image ID’s of the images on the Docker host.
Example
Output
When we run the above command, it will produce the following result −
docker inspect
This command is used see the details of an image or container.
Syntax
Download Hello World Docker Image Size
Options
Repository − This is the name of the Image.
Return Value
The output will show detailed information on the Image.
Example
Output
When we run the above command, it will produce the following result −
Docker and Golang make perfect partners, as we'll see. Let's start by getting you set up with the right tools.
Install Docker
Docker is a tool for building and running containers (you can read more about these in the Cloud Native DevOps with Kubernetes book). A container is a simple way of distributing software, using a single file called a container image.
The technical details don’t really matter for our purposes, but you can think of a container image as being like a ZIP file. It’s a single binary file that has a unique ID and holds everything needed to run the container. This means that if you upload your container to a public repository such as Docker Hub, you can run it anywhere using just Docker. We'll see how to do that shortly.
If you don't have Docker installed already, let's take care of that first. If you have a Mac or a Windows PC, you can download the 'Docker Desktop' application, which has everything you need:
For other platforms, see the Docker Community Edition downloads page.
Once you've installed Docker, run the application, and you'll be all set for the next steps.
Install Golang
If you're new to Go, then welcome! (If you'd like some help learning Go, see my Learn Go with Mentoring page.) Install python packages mac anaconda.
Go is a modern programming language (developed at Google since 2009) that prioritizes simplicity, safety, and readability, and is designed for building large-scale concurrent applications, especially cloud native web services. It’s also a lot of fun to program in. (We often refer to Go as 'Golang', because that's easier to search for, but the name of the language is actually just 'Go'.)
To get started programming with Go, you'll need the 'Go tools' installed: that's the Go compiler itself, libraries, supporting tools, and so on. If you're on Mac, the simplest way to install Golang is to run:
If not, or if that doesn't work for you, follow the instructions for your platform at the Go project's Getting Started page. On Linux, for example, you should be able to install Go via your system's package manager:
Clone the Docker Golang example
If you already have a Go program you'd like to create a Docker container from, great! You can use that. If not, here's one I prepared earlier:
This is a GitHub repo which accompanies the Cloud Native DevOps with Kubernetes book (you can read more about it at the Cloud Native DevOps blog). It's a very simple demonstration app: a little HTTP server which displays 'Hello, world' in your browser.
If you don't have Git installed, see the Download Git page for instructions. Then run these commands to clone (download) the example repo:
Start with the hello
subdirectory. Here you'll find a main.go
file, which looks like this:
Don't worry if this doesn't make a lot of sense to you right now; a lot of it is essentially boilerplate. Go isn't a difficult language to learn, so if you're new to it, the only bit you need to look at is this line:
As the print
suggests, this prints the message Hello, 世界
in the browser. (Why in Chinese? Well, why not?)
Build your Go program
Go is a compiled language, which means that we need to build (compile) our program in order to turn it into a binary file which you can run. We do that, straightforwardly enough, using the go build
command:
You'll find (assuming everything's set up correctly) that you now have a file named hello
in the current directory. Run this:
You won't see any output, but if you go to your web browser and browse to this URL:
you should see the friendly message we were expecting:
Hello, 世界
Build your Golang Docker container image
So, now that you have a kick-ass Go program, how do you turn it into a Docker container? You use the docker image build
command, which takes as input a special text file called a Dockerfile. The Dockerfile specifies exactly what needs to go into the container image.
Here's the Dockerfile for our example Golang program:
A Golang Dockerfile usually looks something like this, but the details may vary. Here the FROM golang..
line specifies the base image to start with (this contains the Go tools and libraries, ready to build your program). The COPY
command tells Docker to copy the Golang source code from the current directory into our container. Then the RUN ..
command tells Docker how to build it.
golang:1.14-alpine
specifies the Alpine version of the Go base image. Alpine is a tiny Linux distribution designed specifically for containers. So Docker, Golang, and Alpine are made for each other!
We have a second FROM SCRATCH
line in this Dockerfile, which tells Docker to start again with a fresh, completely empty container image (called a scratch container), and copy our compiled demo
program into it. This is the container image that we'll then go on to run later.
Using a scratch image saves a lot of space, because we don't actually need the Go tools, or anything else, in order to run our compiled program. Using one container for the build, and another for the final image, is called a multistage build. Docker multistage builds for Golang programs are common, because the compiled binary is so small compared to the Golang base image.
To build the image, run this command:
This is very exciting! You've just built your first Golang Docker image! Now we're ready to run it.
Run your container image
Run the command:
Again, you won't see any output, but again, if you browse to http://localhost:8888
, you should see the friendly, multilingual greeting message:
Hello, 世界
Next steps
Your Golang Docker image is available on your computer to run any time you want to. If you'd like to try modifying the program, feel free! Edit main.go
and change the message to anything you like, for example. When you're ready to rebuild your container, run the docker image build -t hello .
command again.
To make your container more widely useful, you can create a Docker Hub account and upload your image to a public repository. Then anyone in the world can run your program just by using the docker container run
command, or they can run it on any platform that supports Docker containers, such as Kubernetes.
Download Hello World Docker Image File
(You can also use Docker Compose, docker stack
, or docker deploy
to run Golang containers, but we haven't got space to talk about those here. See the Docker documentation for details.)
Docker Golang development is a good way for programmers to work, because you can build and test the container image on your own machine, before sending it to be deployed on whatever platform you use to run your applications. Because the container holds everything needed to run the program, there's no chance of running into the 'it works on my laptop!' problem.
Docker Hello World Image Download
Have fun!
More about Go
If you're interested in learning Go, you're in the right place: I have lots of Go tutorials on this very site. Check them out!
You can buy my introductory ebook on Go for a very reasonable $4.99; it's called For the Love of Go: Fundamentals.
If you like videos, I've put together a set of YouTube playlists with some great talks by Go experts. You can find them here:
- Golang University 101 introduces fundamental Go concepts and shows you how to use the Go tools to create and manage Go code
- Golang University 201 steps it up a notch, explaining important techniques like testing, web services, and APIs
- Golang University 301 dives into more advanced topics like the Go scheduler, implementation of maps and channels, and optimisation techniques
And if you'd like some one-to-one or team training in Go, get in touch! Email go@bitfieldconsulting.com, or see my Learn Golang page for more details.