Web applications have come a long way, and so have the services that host them. One of the most popular ones now is Docker. The concept of sandboxed containers for web applications gives so much more accessibility and control. However, when you’re just starting out on using Docker, it might take some time to get used to. Usually, when building images for Docker using a Dockerfile, you might run into the following error:
/bin/sh: apt-get: not found
Which is why in this article, I will help you cure this error and get your docker image up and running in minutes.
You’re using the wrong command
Yes. In fact, it is most probably the case for which you have faced the issue. C’mon Sarker! What are you saying?
As it appears to be, it is common to confuse the package manager of Debian-based distributions (such as Ubuntu), with the package manager of Alpine Linux. Alpine Linux is the go-to distribution for use in docker containers. And if you happen to follow any online tutorials on using Docker for setting up your containers, it is very much possible that you had used the alpine linux image (without realizing it) and are now trying to use the package manager of some other distribution.
In Alpine Linux, the package manager is called apk, while in Debian-based distributions like Ubuntu, the package manager is called apt. If you now try to use apt inside an alpine linux container (or inside a Dockerfile defined with alpine), you’d get the error message.
So basically, there are two main fixes for this. The first is to use the right package manager for your container, without changing the distribution, and the other option is to entirely change the distribution to something like Ubuntu, so it no longer bothers you. The following sections will discuss them in detail.
Use the right package manager
If you are fine with using a new package manager for your system that you’re not used to, it’s best to simply just learn it. If you had selected alpine linux when building your image, you need to use the apk package manager. The base differences of it from the apt or the apt-get commands in Ubuntu are as follows:
- When trying to update and upgrade the system, you’d use the following command:
$ apk update && apk upgrade
- When you want to install a program, use the following command:
$ apk add <program_name>
- To check which packages have an update available, use this command:
$ apk version -v
- To update only particular packages instead of updating system-wide, use:
$ apk add -u <package_name_1> <pakage_name_2> ……
- When removing packages, use the following command:
$ apk del <package_name>
There are a few other differences, but these are the ones you’d mostly need to use on a daily basis. So try to stick with it (if you wish to use docker containers that are lightweight).
In case you’re using a Dockerfile defined with alpine linux, you need to make sure that you use apk if you include anything that relates to the package manager instead of using apt. For example, if you wish to include lines in your Dockerfile such as one which updates the image when being built as well as another one which installs a given package, it should look something like this:
FROM alpine:latest RUN apk update && apk upgrade RUN apk add <package_name>
Using Ubuntu Image in Docker
Directly install and run a container with Ubuntu
If you have decided that the apk package manager is not your thing, and would like to go back to your old, lovely distro (Ubuntu, most probably – why are you reading this if it’s not?) then you can consider building a new image with Ubuntu. If you don’t wish to use a docker file and would like to jump straight in, simply run the following line of command in your terminal (make sure that the docker daemon is running, otherwise you’d encounter another weird error message):
$ sudo docker run -it ubuntu:latest
This command will pull in the latest version of ubuntu from the docker repositories and will create an image for you to run. Once the download is finished, it’ll take you straight inside the container with a totally isolated filesystem. Inside here, you can now do anything you might have wished to do with your container. Why not start by running “apt-get update”? (Chill out. You won’t be seeing the error message this time around!)
Install Ubuntu using a Dockerfile
If you’ve got a good grasp of creating Dockerfiles (even if you don’t, I bet it’s simpler than you may have thought), I’d recommend you to use a Dockerfile to build your image. What this will allow you is that everytime you want to build containers with the same configuration and pre-installation procedures, it will save you a ton of time.
First up, create a file with the exact name “Dockerfile” using the text editor of your choice and store it inside a recognisable folder. Then, you need to include the code (or plainly, some instructions) which will build the Ubuntu image. A very basic Dockerfile to build an Ubuntu image should look like this:
FROM ubuntu:latest RUN apt-get update && apt-get upgrade RUN apt-get install <some_package>
Save and then exit the file. Now while you’re inside the same directory where the Dockerfile is located, run the following command to build the image:
$ sudo docker build -t <name_of_image>
Replace <name_of_image> with anything you like, but you’d probably want it to be short and simple, so it is easy to type for when you want to run the container at a later time.
Once the downloading is over, and all the commands have successfully been executed from the Dockerfile, your image will be built, and you can verify it by running:
$ sudo docker images
You should see a new entry with the name you gave the image, including an Image ID.
To now run and enter your newly created container (Ahh, Ubuntu! Where have you been?), simply use the following command:
$ sudo docker run -it <name_of_image>