'Docker Container doesn't run after creating an image

I'm very new to Docker, so apologies for my lack of knowledge.

The Docker I'm creating is for a Python Django project. I'm also using Celery, RabbitMQ, and MySQL. To this end, I created a Dockerfile:

# A dockerfile must always start by importing the base image.
FROM ubuntu:20.04

# Identify myself as the maintainer:
LABEL maintainer="[email protected]"

# Set Timezone
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Add the "all Python versions" repo:
RUN apt-get update && apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install python3.8
RUN apt-get -y install python3-pip 
RUN apt-get autoremove -y
RUN python3 -m pip install --upgrade pip
RUN pip install setuptools==58

# Create a folder for the Match requirements file:
RUN mkdir /install
WORKDIR /install
# Copy the requirements file:
COPY reqs.txt /install

# Install the requirements using PIP
RUN pip3 install --no-deps -r reqs.txt

# Installing MySQL Server
RUN apt-get -y install mysql-server

# Install RabbitMQ
COPY rabbit-mq-install.sh /install
RUN chmod +x rabbit-mq-install.sh
RUN sh rabbit-mq-install.sh

# Expose the port 8888 for Django Server and expose 3306 for MySQL
EXPOSE 8888
EXPOSE 3306

# Running services@
CMD service mysql start && /etc/init.d/rabbitmq-server start

# Setting Bash as the entry
ENTRYPOINT /bin/bash

I then build the Image using:

docker build -t [MYNAME] .

And try to start a Container using:

docker run  -i -t [MYNAME]

I can see that this creates a new container, but it doesn't stay running.

I have also used the Docker desktop app on Mac, which means I get the instance to run, but the services I've installed and tried to start aren't running.

Appreciate your help and advice!

// edited as I needed to have the -it before the image name.



Solution 1:[1]

Thanks, while this worked in actually putting me in the shell correctly, I still don't see my services running. –

When you pass in arguments on the command line (like /bin/bash), you override the CMD setting in your Dockerfile. If you want a shell into your container and you want your services to start, you would docker run the image without providing any additional arguments, and then you would use docker exec to start a shell in the running container...

...but this isn't going to solve your problem, because you're trying to use the service command to interact with a service manager like init or systemd, and there isn't one running inside your container. The only processes running in your container are those explicitly started via your ENTRYPOINT or CMD entries.

For what you're doing -- starting mysql and rabbitmq -- a much better solution would be to use the official images for those tools:


If you were to design your own image for running mysql, for example, your CMD entry would look something like:

CMD ["mysqld"]

...but if you look at the Dockerfile from the official image, you'll note there's a great deal of initialization that happens first, primarily in the entrypoint script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 larsks