Docker as we all know creates containers which encapsulate the environment needed for an application to run. In this post we summarize the most commonly used commands.
Retrieve an image
An image is nothing but a blueprint needed to setup a container. If a container is a process then the image can be considered as the recipe to create this process. We do this as follows :
docker pull nginx
In this example an image of the popular webserver “nginx” is being pulled from the Docker Hub which can be found at hub.docker.com
Running a container
Once the nginx image has been retrieved we then use
docker run -it -p 3000:80 –name mynginxserver nginx
to start up the nginx container and get the webserver running. In the above command the option it makes the container run interactively and the p option exposes the port on the container we can use to talk to our mynginxserver. As you would have guessed the –name option gives our container a name and we also specify the image that we are using to run our container at the very end which in this case is “nginx”.
Listing images and containers
These are very common and easily remembered commands.
will list all the images that you have in your local setup
docker container ls
will list all the containers which are actively running
docker container ls -a
will list all the containers in your setup
Stopping a container
When you no longer need a container you can use the following command
docker container stop mynginxserver
This stops the container running my NGINX server
A container which is no longer needed can be removed using :
docker rm mynginxserver
where mynginxserver is the name of my container
Images which are no longer needed can be removed using either of the following commands. An image can be removed only if it is not in use.
docker rmi imageid
where imageid is the id of the image that you would like to remove
docker image rm imagename
where imagename is the name of the image you are using
Creating a dockerfile
A dockerfile contains all the information needed to build an image. Let’s look at a dockerfile used to create a Node application server
COPY package.json ./app
COPY app.js ./app
RUN npm install
CMD ["node", "app.js"]
The code above has to be saved in a file named “dockerfile” and can be placed in the same folder as your Node application project files.
The code shown above downloads the latest node image from the Docker Hub, copies package.json and app.js into a working directory called app and then runs the npm install command to install all the dependent packages needed by your Node application.
We then run the node server using the node command and supply the app.js file to it.
The final line with EXPOSE directive informs Docker that the Node server is listening on port 3000.
Building an image from a dockerfile
We then use this file and build an image using the command shown below
docker build . -t mynodeserverimage
The command above when run from the same directory as “dockerfile” will create an image with the name “mynodeserverimage”. Image names should all be in lowercase.
Starting up a container using this image can be done in the usual way by running
docker container run -it -p 3823:3000 –name mynodeserver mynodeserverimage
The command above starts up a container named mynodeserver and exposes port number 3823 on the container which can be used to talk to the server.
And that wraps up our quick introduction to Docker.