My task is to create multiple containers from a single docker image. The following steps I did(ubuntu)
$ mkdir docker-git
$ cd docker-git
$ touch Dockerfile
Now inside the Docker file I edited following commands
Dockerfile:
FROM ubuntu:16.04
MAINTAINER madhan.devops1@gmail.com
RUN apt-get clean
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get install -y git
ENTRYPOINT /bin/bash
then I built the image with the following command and image created successfully
$ docker build -t docker-git .
then I have created a docker-compose file as following
docker-compose.yml
version: ‘3’
services:
docker-git-container:
image: docker-git
Now, As Dockerfile and docker-compose.yml are created I gave the following command
$ docker-compose up --scale docker-git-container=3
The above command created 3 containers but, I am not able to enter into the container with the following command
$ docker exec -it eda29149d917 /bin/bash
Error response from daemon: Container eda29149d917f9d61c0dcef6396a913bfa81f7126e20fd9583c9d4cfb5218d8c is not running
Though I have started the container still not able to enter into it. Please help me?
Your Docker image doesn’t actually do anything: you haven’t installed any software in it besides a git client. You should look at the official Docker tutorial on building and running custom images.
In my experience, best practice is to put the Dockerfile in the repository’s root directory, and COPY your built application into the container. Get your application working without Docker, including passing all of its local tests, and only then start worrying about building an image. It’s hard for me to think of a use for installing git in an image that doesn’t result in trouble (certainly git clone inside the Dockerfile will not work well).
Try to avoid using docker exec, handy as it may be. Think of it as the equivalent of getting a root shell on your production server: it’s important to have around for emergencies, but not how you want to be doing work most of the time.
As David mentioned, when you launch your container, there is no foreground process running so it will start and then immediately stop. This is expected behaviour. There’s also nothing wrong in having a container that does that. For example a common pattern when creating a image to act as part of your tool-chain is to launch, perform its task (often defined with a combination of ENTRYPOINT and CMD) and then exit (cleaning up the launched container after). We use that for all sorts of things, for example, ephemeral Jenkins agents, infrastructure build tools like Packer and Terraform, and so on. You haven’t described what your application use case here, so if it’s one of those, that’s fine, you just need to sort out what happens when you run it. More typically of course, a container runs (and continues to run) an application until you explicitly tell it to stop (or it fails and might be replaced). In that case your container needs a process to run when it starts up. Again ENTRYPOINT and/or CMD is the most typical way of achieving that. Yours just open a command prompt, that won’t do !
As far as installing a Git client inside a Docker image, I disagree with David. There’s nothing wrong with that at all if that’s part of the function of the container. However, if it’s there simply to support further build of your eventual image and you don’t need it for the app that will run, then you should look at multi-stage builds so that your final image does have it.
Google is your friend (as are the docker docs - take the time to study them, everything you need to know is there).