Tuesday, February 14, 2023

One reason of "Error response from daemon: Container {id} is not running" error with mongo docker container

In this post I will write about one possible problem which you may face with when try to run MongoDB docker container. Env used in this article: docker installed on Ubuntu running on WSL2 on Windows 11.

Let's start from beginning. If we want to run MongoDB docker container we first need to download it:

docker pull mongo

If everything went well you should see something like that:

Then we may want to run it with ports forwarding (MongoDB by default uses port 27017). At first I tried to do it like that:

docker run -d --name mongo-test mongo:latest -p 27017:27017

It didn't return errors and just echoed container id:

But when I tried to connect to container with bash

docker exec -it mongo-test bash

it failed with the following error:

Error response from daemon: Container {id} is not running


Checking the docker logs for mongo-test container helped to found the reason:

docker logs mongo-test

 Error parsing command line: unrecognised option '-p'

The problem was that port forwarding option was specified at the end while according to documentation of docker run command it should be specified before image name:

When I changed the command like that:

docker run -d -p 27017:27017 --name mongo-test mongo:latest

container successfully ran and I was able to connect to it via bash:

So it is important to check options order when you run docker container. Although there are no error messages it won't work as expected otherwise.



Wednesday, February 8, 2023

One important difference between MySQL installation on Windows and Linux

MySQL may be installed both on Windows and Linux. However there is one important change which I would like to know before to install it on Linux :) In this post I will share this finding.

Under the hood MySQL tables data is stored in files on file system. Windows file system is case insensitive and if you try to create table like that:

create table `UserInfo` (
    ...
)

this table will be created with lowercase name anyway (even though you used Camel case in SQL statement. In this example it will be created with "userinfo" name). But at the same time you may read data from this table using all following queries:

select * from UserInfo
select * from userInfo
select * from userinfo

From other side Linux file system is case sensitive which means that by default table will be created there with exactly the same characters registry which was used in SQL statement. So if table was created using SQL statement above then you need to use exactly the same name also in SQL queries:

select * from UserInfo

Other queries will fail with table not found error:

select * from userInfo
select * from userinfo

If you develop cross-platform application which works with MySQL it may cause compatibility issues: code which works in Windows won't work in Linux because of different characters registry in table names used in SQL queries. In order to avoid this error we may configure MySQL on Linux so all tables will be created with lowercase name - i.e. same way how it works on Windows. It can be done by adding the following configuration setting to /etc/my.cnf.d/mysql-server.cnf file (this example is for Red Hat Linux. For other Linux distributives MySQL config file may be located in other folder e.g. in /etc/mysql and may have different name):

[mysqld]
...
lower_case_table_names=1

NOTE: it should be done during MySQL installation before to run database instance service. It is not possible to change this property when MySQL is already installed (in this case you will need to uninstall it and install again). With this approach code will be the same and will work both on Windows and Linux.