Command to use with scratch docker container - mysql

I'm trying to start a docker container for mysql. The image for the container was built from scratch for a training I attended and I need to figure out how to configure it to run a command that will start the container.
The /bin/bash and /bin/sh commands don't work. When I docker inspect the container the CMD section doesn't contain anything. I've tried running CMD['/bin/bash'] or CMD['/bin/sh'] at the end of my docker container run command and that populates the CMD field but the container still won't run.
There are a number of other microservice containers I'm having the same problem with. This is the first one I need to solve however.
This is the command I'm running:
docker run -d -v infytel-mysql-volume:/var/lib/mysql --network=infytel-docker-networkMS --name=infytel-mysql-con2 -e MYSQL_PASSWORD_ROOT=root infytel-mysql-img:v1 /bin/bash
This is my error:
oci runtime error: container_linux.go:235: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory
[EDIT] Running docker logs gives the error shown above.
Running without the /bin/sh command states error response from daemon: No command specified

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.

Docker mongodb 4.2 not running with bind mount

I'm learning docker and mongodb at the same time, I'm trying to import a collection with a JSON file in a docker container but there is an issue, as this is a container I'm using bind mounts to refer this JSON from my local machine to my container but if I run it with bind mounts mongodb won't run.
I'm using the following command to run just the container using bash:
sudo docker exec -it mongo-image bash
inside the container I just write mongo and terminal from mongo works as expected, if I run the following command:
sudo docker run -it -v "$(pwd)":/MyData mongo:4.2 /bin/bash
I see the files from my local machine but If I type mongo terminal won't let me start
root#e753483bb65b:/# mongo
MongoDB shell version v4.2.15
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2021-07-30T21:05:14.037+0000 E QUERY [js] Error: couldn't connect to server
127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to
127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:353:17
#(connect):2:6
2021-07-30T21:05:14.040+0000 F - [main] exception: connect failed
2021-07-30T21:05:14.040+0000 E - [main] exiting with code 1
Is there any suggestion that you could give me to import a json file collection to mongo db docker container or fix this issue?
I think you are overwriting the default run command of mongo and it has nothing to do with mounting and volumes. I suggest you change this:
sudo docker run -it -v "$(pwd)":/MyData mongo:4.2 /bin/bash
in two separate commands:
sudo docker run -d -v "$(pwd)":/MyData mongo:4.2
and then when it printed the id of the running container, create an interactive shell:
sudo docker exec -it fb82f /bin/bash
where fb82f is the initial characters of the docker container.

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

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.

Docker: Edit "my.cnf" file in stopped container

After making an edit to "my.cnf", I now get an error from Kitematic on the Mac when I attempt to start the container:
mysqld: [ERROR] Found option without preceding group in config file /etc/mysql/my.cnf at line 19! mysqld:
[ERROR] Fatal error in defaults handling. Program aborted!
I've tried accessing the container via:
docker exec -it [container] bash
... but I get the error:
Error response from daemon: Container [container] is not running
I was able to access something via the image, but the file didn't appear to be the same, so I'm not sure what was happening (I'm not too conversant with Docker).
At this stage, either making the appropriate edit and fixing the container, or somehow cloning the MySQL data to another container would be ideal.
To fix the my.cnf, you can use docker container cp. It works with stopped containers.
To copy file from your container to current path
docker container cp containerId:/etc/mysql/my.cnf container-my.cnf
Then edit container-my.cnf and copy back from path to container :
docker container cp container-my.cnf containerId:/etc/mysql/my.cnf
To use the existing MySQL data with a new container:
docker container inspect -f '{{.Mounts}}' [container]
gives you the volume name (key volume) where the data is. Then start a new mysql container and mount the volume under /var/lib/mysql:
docker container run -d -v [volume_name]:/var/lib/mysql [image]
Afterwards you can remove the old container (Actually you can remove it before creating the new one)
If you are using the official mysql docker image from
https://hub.docker.com/_/mysql
you can use a custom MySQL configuration file that overrides my.cnf
If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

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.