why dockerfile can't start db with " CMD /etc/init.d/mysql start "? - mysql

I downloaded a docker image with mariadb and phpmyadmin,
then wrote two dockerfiles below..
# dockerfile A
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin
CMD /etc/init.d/mysql start && /etc/init.d/apache2 start
# dockerfile B
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin
CMD service mysql start && /usr/sbin/apachectl -D FOREGROUND
dockerfile B worked well,
but dockerfile A failed.
I can build image from dockerfileA,
then spin-up container from it docker run -it -p 80:80 <img id> bash
the container up successfully,
but while I inside the container, I found the services of mariadb and apache2 not working.
After I execute /etc/init.d/mysql start && /etc/init.d/apache2 start,
mariadb and apache2 works!
Trying to get error messages by docker logs <container id>, but got nothing.
What my question is
"If I run the docker image without dockerfile just by commands,
like what I did in dockerfile A. The container works well. "
$ docker run -it -p 80:80 alfvenjohn/raspbian-buster-mariadb-phpmyadmin bash
$ /etc/init.d/mysql start && /etc/init.d/apache2 start
Why? Didn't dockerfile A do the same thing, as I spin up my container with commands ?

You need to remove the bash at this end of the command. This replace the command inside your dockerfile.
docker run -d -p 80:80 <img id>
You can use this command to connect inside the container afterward:
docker exec -it <container_id> bash

A Docker image runs a single command only. When that command exits, the container exits too.
This combination usually means a container runs a single server-type process (so run MySQL and Apache in separate containers), and it means the process must run in the foreground (so the lifetime of the container is the lifetime of the server process).
In your setup where you launch an interactive shell instead of the image CMD, you say you can start the servers by running
/etc/init.d/mysql start && /etc/init.d/apache2 start
This is true, and then you get a command prompt back. That means the command completed. If you run this as an image's CMD then "you get a command prompt back" or "the command completed" means "the container will exit".
Generally you want to launch separate database and Web server containers; if you have other application containers you can add those to the mix too. Docker Compose is a common tool for this. You may want to look through Docker's sample applications for some examples, or other SO questions.

Related

Resuming docker mysql instance after restarting

I'm using docker to run a mysql 5.6 instance on my localhost (which is running ubuntu 20.04), using these instructions. When I create a new container for the database I use the following command
sudo docker run --name mysql-56-container -p 127.0.0.1:3310:3306 -e MYSQL_ROOT_PASSWORD=rootpassword -d mysql:5.6
That serves the intended purpose; I'm able to create the database using port 3310 and get on with what I want to do.
However when I reboot my localhost, I am unable to get back into sql5.6 using that port again.
When I list containers, I see none listed:
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
So I try to recreate it and am told that it already exists:
$ sudo docker run --name mysql-56-container -p 127.0.0.1:3310:3306 -e MYSQL_ROOT_PASSWORD=rootpassword -d mysql:5.6
docker: Error response from daemon: Conflict. The container name "/mysql-56-container" is already in use by container "a05582bff8fc02da37929d2fa2bba2e13c3b9eb488fa03fcffb09348dffd858f". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
So I try starting it but with no luck:
$ sudo docker start my-56-container
Error response from daemon: No such container: my-56-container
Error: failed to start containers: my-56-container
I clearly am not understanding how this works so my question is, how do I resume work on databases I've created in a docker container after I reboot?
docker ps just list running containers. If you reboot your laptop, all of them will be stopped. You can use docker ps --all or docker container ls --all to list all containers (running or stopped). You can check more about the docker ps command in docker ps command line reference
Once a container is created, you cannot create another with the same name. Tha is the reason your second docker run is failing.
You should use docker start instead. But you are trying to start a container with a different name. Your docker start command is using a container named my-56-container but it is called mysql-56-container. Please check your first docker run command in the question.

Dockerfile to install Nginx and MySQL in same image

I want to install nginx and mysql in same image. I start out with a mysql image with the plan to install docker using dockerfile.
Here is my dockerfile:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD=HelloWorld \
MYSQL_DATABASE=content
RUN apt update
RUN apt install nginx -y
COPY nginx.conf /etc/nginx/nginx.conf
This starts the mysql db perfectly and nginx also gets installed. Unfortunately, nginx doesn't start. To start nginx I also added another command in the docker file:
CMD service nginx start
After adding this line in the dockerfile, the container closes after creation. What am I doing wrong here?
I am using below command to start container with above image:
docker run -it -p 3306:3306 -p 8080:80 -p 8081:443 --name mycontainer myimage
it's best to run each process in a separate container. but if you wanna do that, you should create a bash file to start MySQL and Nginx. finally, you should use that bash file as the ENTRYPOINT of your image/container

Dockerfile and background running mysql server

i have problems...
Firstly i have a Dockerfile where i define all the steps, like updating system, installing mysql, change mysql root password.
Then i set an EntryPoint so my container on start will exec mysql server.
I have 2 problems:
- When i start the container, it restarts every 10 seconds.
- When i use exec to enter the docker it says: "No docker with such id".
This is my Dockerfile:
# Set the base image
FROM ubuntu:14.04
MAINTAINER redigaffi
RUN apt-get update \
&& apt-get -y install mysql-server \
&& service mysql start \
&& mysqladmin -u root password FEGj5nmKYRha
ENTRYPOINT service mysql start \
&& bash
#VOLUME /root/mysql:/var/lib/mysql:rw Please run -v running this docker since Dockerfile has not access to host files
EXPOSE 3306
I put bash on the end on the entrypoint because without it container just closes, so it remains in background.
I have tried many commands to execute this container:
docker run -d df0bb600c10f /bin/bash # This one closes the container after 2 seconds
docker run -d --restart=always df0bb600c10f /bin/bash # This one remains, but restarts every 10 seconds and i cant access this docker using exec.
Please help, what is wrong ?
Thank you!
Try using the supervisor. This article here shows the steps.

mysql with Exited(1) from docker

Start learning docker and try to setup a mysql container. But it dies immediately with Exited(1).
Following is the command used
docker run mysql -e MYSQL_ROOT_PASSWORD=password1
Looking at docker ps, it does not show any running docker container
with docker ps -a returns the following :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e681f56c52e2 mysql "/entrypoint.sh -e MY" 3 seconds ago Exited(1) 3 seconds ago lonely_rosalind
Nothing shows up for docker logs lonley_rosalind either
Any idea how to determine why if failed ?
I am running
ubuntu 15.04
docker version 1.9.1 build a34a1d5
Try this
docker run -e MYSQL_ROOT_PASSWORD=password1 mysql
When you are writing something after docker image name docker accepts it as a command for execution in your created container. Pattern for docker run:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run mysql instance in Docker container

I want to run a mysql container in Docker. The Dockerfile that I use is the Dockerfile defined in the official repo[here].
I only extended this Dockerfile with 2 more lines so I can import a init sql file, like this :
ADD my-init-file.sql /my-init-file.sql
CMD ["mysqld", "--init-file=/my-init-file.sql"]
I want to run this instance as a daemon but when I execute this command, from the documentation:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
The container exits automatically. I want to run it as a daemon so I can link apps(like a wordpress site) in another container to the mysql database.
Maybe I am missing something. Can anyone show me how ?
[EDIT] I forgot to say that I ran docker logs my-container after starting the container and there is no error :
Running mysql_install_db ...
Finished mysql_install_db
docker ps shows no running container.
My guess is the command executes successfully but the mysqld daemon does not start.
Your Dockerfile seems fine. Your init file may be buggy, though. If MySQL terminates, then the container will terminate.
The first debug step is to look at the logs:
docker logs some-mysql
You can use this whether the container is stopped or running. Hopefully, you'll see something obvious, like you missed some semicolons.
If the logs don't help, the next thing to try is to get inside the container and see what's happening first-hand
docker run -e MYSQL_ROOT_PASSWORD=mysecretpassword -it mysql /bin/bash
This will get you a Bash shell inside your container. Then you can run
mysqld --init-file=/my-init-file.sql
And see what happens. Maybe something in your init file tells MySQL to exit cleanly, so you get no logs but the command terminates.
Dmitri, after you made docker run with -d argument your container detached and already working as daemon if only CMD command not returned exit code.
You can check running containers by docker ps command.
You can check all containers by running docker ps -a.
Also i think you will need to open mysql port outside the container. You can do it with -P argument or better way to make communication between containers is docker links.